/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mvc.example.presentation.controller;

import java.util.logging.Level;
import java.util.logging.Logger;
import mvc.example.bussines.BussinesFactory;
import mvc.example.bussines.ManagerException;
import mvc.example.bussines.domain.Student;
import mvc.example.presentation.events.DataEvent;
import mvc.example.presentation.listeners.StudentActionListener;
import mvc.example.presentation.listeners.StudentDataListener;
import mvc.example.presentation.model.StudentsModel;
import mvc.example.presentation.view.StudentView;

/**
 *
 * @author fernando.hernandez
 */
public class StudentController implements StudentActionListener{
    
    private StudentsModel studentsModel;
    boolean edit;
    
    public StudentController(StudentsModel studentsModel) {
        this.studentsModel = studentsModel;
        init();
    }
    
    
    private void init(){
        StudentView studentView = new StudentView(this);
        studentView.setVisible(true);
    }

    public StudentsModel getStudentModel(){
        return studentsModel;
    }

    @Override
    public void saveStudent(DataEvent e) {
        try {
            Student student =(Student) e.getSource();
            BussinesFactory.getStudentManager().saveStudent(student);
            if(edit){
                getStudentModel().remplaceStudent(student);
            }else{
                getStudentModel().addStudent(student);
            }
        } catch (ManagerException ex) {
            Logger.getLogger(StudentController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void removeStudent(DataEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean isEdit() {
        return edit;
    }

    
}
